package com.mao.ui;

import com.mao.bean.employee;

import javax.swing.*;
import java.awt.*;

public class AddEmployeeUI extends JFrame {
    private EmployeeManagerUI employeeManagerUI;
    public AddEmployeeUI(EmployeeManagerUI employeeManagerUI) {
        this.employeeManagerUI = employeeManagerUI;
        // 窗口设置
        setTitle("员工信息录入系统");
        setSize(450, 460);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        getContentPane().setBackground(new Color(245, 245, 245));

        // 主容器使用GridBagLayout
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 30, 20, 30));
        mainPanel.setBackground(Color.WHITE);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(8, 8, 8, 8);

        // 组件样式配置
        Font labelFont = new Font("微软雅黑", Font.PLAIN, 14);
        Font fieldFont = new Font("宋体", Font.PLAIN, 14);
        Dimension fieldSize = new Dimension(200, 28);

        // 输入字段配置
        String[] labels = {"ID：", "姓名：", "性别：", "年龄：", "电话：",
                "职位：", "入职时间：", "薪水：", "部门："};
        JTextField[] fields = new JTextField[labels.length];
        JComboBox<String> genderCombo = new JComboBox<>(new String[]{"男", "女"});
        genderCombo.setFont(fieldFont);
        genderCombo.setPreferredSize(fieldSize);

        // 构建输入行
        for (int i = 0; i < labels.length; i++) {
            // 标签配置
            JLabel label = new JLabel(labels[i]);
            label.setFont(labelFont);
            label.setForeground(new Color(64, 64, 64));

            // 输入组件配置
            Component inputComp;
            if (i == 2) {
                inputComp = genderCombo;
            } else {
                fields[i] = createStyledField(fieldFont, fieldSize);
                inputComp = fields[i];
            }

            // 布局设置
            gbc.gridy = i;
            gbc.gridx = 0;
            gbc.weightx = 0;
            gbc.anchor = GridBagConstraints.WEST;
            mainPanel.add(label, gbc);

            gbc.gridx = 1;
            gbc.weightx = 1;
            mainPanel.add(inputComp, gbc);
        }

        JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 15, 10));
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 15, 15));

        // 添加按钮
        JButton addButton = new JButton("保存");
        addButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
        addButton.setBackground(new Color(70, 130, 180));
        addButton.setForeground(Color.WHITE);
        addButton.setFocusPainted(false);

        // 给保存按钮绑定一个点击事件监听器。
        addButton.addActionListener(e -> {
            // 获取输入框的数据，封装成员工对象，添加到信息界面的集合并在其表格展示。
            employee employee = new employee();
            employee.setId(Integer.parseInt(fields[0].getText()));
            employee.setName(fields[1].getText());
            employee.setSex(genderCombo.getSelectedItem().toString());
            employee.setAge(Integer.parseInt(fields[3].getText()));
            employee.setPhone(fields[4].getText());
            employee.setJob(fields[5].getText());
            employee.setEntryTime(fields[6].getText());
            employee.setSalary(Double.parseDouble(fields[7].getText()));
            employee.setDept(fields[8].getText());
            // 把这个员工对象送到信息界面那里去，添加到信息界面的集合中，并在信息界面的表格中展示。
            employeeManagerUI.addEmployee(employee);
            // 弹出一个提示添加成功的弹框
            JOptionPane.showMessageDialog(this, "员工信息添加成功！");
            //  关闭当前窗口
            this.dispose();
        });

        // 取消按钮
        JButton cancelButton = new JButton("取消");
        cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        cancelButton.setBackground(new Color(220, 220, 220));
        cancelButton.addActionListener(e -> dispose());

        // 按钮样式统一设置
        for (JButton btn : new JButton[]{addButton, cancelButton}) {
            btn.setPreferredSize(new Dimension(90, 30));
            btn.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
        }

        panel.add(addButton);
        panel.add(cancelButton);

        // 按钮面板
        JPanel buttonPanel = panel;



        // 整体布局
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(mainPanel, BorderLayout.CENTER);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        setVisible(true);
    }

    // 创建样式化的输入框
    private JTextField createStyledField(Font font, Dimension size) {
        JTextField field = new JTextField();
        field.setFont(font);
        field.setPreferredSize(size);
        field.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                BorderFactory.createEmptyBorder(0, 5, 0, 5)
        ));
        return field;
    }

}